package util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author Fabian (fabian@youleyu.com)
 * @version 1.0 
 * @create date May 10, 2010
 * @description 密码加密
 */
public class SecurityTools {

	private static final byte salt[] = "fabian".getBytes();

	public SecurityTools() {

	}

	public static String digest(String str) {
		MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("SHA");
			md5.update(salt);
			return new BASE64Encoder().encode(md5.digest(str.getBytes()));
		} catch (NoSuchAlgorithmException ex) {
			throw new UnsupportedOperationException(ex.toString());
		}
	}

	public static void md5(byte data[], int offset, int length, byte digest[], int dOffset) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(data, offset, length);
			md5.digest(digest, dOffset, 16);
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
		} catch (DigestException ex) {
			ex.printStackTrace();
		}
	}

	public static byte[] md5(byte data[], int offset, int length) {
		try {
			MessageDigest md5;
			md5 = MessageDigest.getInstance("MD5");
			md5.update(data, offset, length);
			return md5.digest();
		} catch (NoSuchAlgorithmException ex) {
			ex.printStackTrace();
			return null;
		}
	}

	public static byte[] encrypt(byte key[], byte src[]) {
		try {
			return getCipher(key, 1).doFinal(src);
		} catch (BadPaddingException ex) {
			throw new UnsupportedOperationException(ex.toString());
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String encrypt(String key, String src) {
		try {
			return new BASE64Encoder()
					.encode(getCipher(key.getBytes("UTF8"), 1).doFinal(
							src.getBytes("UTF8")));
		} catch (UnsupportedEncodingException ex) {
			throw new UnsupportedOperationException(ex.toString());
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static byte[] decrypt(byte key[], byte src[]) {
		try {
			return getCipher(key, 2).doFinal(src);
		} catch (IllegalBlockSizeException ex) {
			throw new UnsupportedOperationException(ex.toString());
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String decrypt(String key, String src) {
		try {
			return new String(getCipher(key.getBytes("UTF8"), 2).doFinal(
					new BASE64Decoder().decodeBuffer(src)), "UTF8");
		} catch (UnsupportedEncodingException ex) {
			throw new UnsupportedOperationException(ex.toString());
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Cipher getCipher(byte key[], int mode) {
		try {
			Cipher c;
			if (key.length < 8) {
				byte oldkey[] = key;
				key = new byte[8];
				System.arraycopy(oldkey, 0, key, 0, oldkey.length);
			}
			SecretKeyFactory keyFactory;
			java.security.spec.KeySpec keySpec;
			if (key.length >= 24) {
				keyFactory = SecretKeyFactory.getInstance("DESede");
				keySpec = new DESedeKeySpec(key);
				c = Cipher.getInstance("DESede");
			} else {
				keyFactory = SecretKeyFactory.getInstance("DES");
				keySpec = new DESKeySpec(key);
				c = Cipher.getInstance("DES");
			}
			javax.crypto.SecretKey k = keyFactory.generateSecret(keySpec);
			c.init(mode, k);
			return c;
		} catch (NoSuchAlgorithmException ex) {
			throw new UnsupportedOperationException(ex.toString());
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

//	public static void main(String args[]) {
//		//System.out.println(digest("bangbang"));
//		System.out.println(encrypt("haha","123456"));
//		System.out.println(encrypt("haha","bangbang"));
//	}

}